﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;

namespace RegionSplittor.Utils.InternalCorner
{
	public class InternalCornerData
	{
		public InternalCornerData(XYZ ptIntersect, Curve firstEdge, Curve secondEdge, List<Curve> lstClosedCurve)
		{
			this.m_intersectPt = ptIntersect;
			if (firstEdge.Length > secondEdge.Length)
			{
				this.m_maxEdge = firstEdge;
				this.m_minEdge = secondEdge;
			}
			else
			{
				this.m_maxEdge = secondEdge;
				this.m_minEdge = firstEdge;
			}
			XYZ maxDistancePt = CurveUtils.GetMaxDistancePt(this.m_maxEdge, ptIntersect);
			XYZ maxDistancePt2 = CurveUtils.GetMaxDistancePt(this.m_minEdge, ptIntersect);
			this.m_vecMaxDirection = (maxDistancePt - ptIntersect).Normalize();
			this.m_vecMinDirection = (maxDistancePt2 - ptIntersect).Normalize();
			this.m_maxIntersectEdge = this.GetIntersectCurve(this.m_maxEdge, lstClosedCurve);
			this.m_minIntersectEdge = this.GetIntersectCurve(this.m_minEdge, lstClosedCurve);
			this.m_ptMaxEnd = CurveUtils.GetMaxDistancePt(this.m_maxEdge, this.m_intersectPt);
			this.m_ptMinEnd = CurveUtils.GetMaxDistancePt(this.m_minEdge, this.m_intersectPt);
		}

		private Curve GetIntersectCurve(Curve curveSrc, List<Curve> lstClosedCurve)
		{
			Curve result = null;
			foreach (Curve curve in lstClosedCurve)
			{
				IntersectionResultArray intersectionResultArray = null;
				curveSrc.Intersect(curve, out intersectionResultArray);
				if (intersectionResultArray != null && intersectionResultArray.Size == 1 && !curve.Equals(this.m_minEdge) && !curve.Equals(this.m_maxEdge))
				{
					result = curve;
				}
			}
			return result;
		}

		public XYZ m_intersectPt;

		public XYZ m_ptMaxEnd;

		public XYZ m_ptMinEnd;

		public Curve m_maxEdge;

		public Curve m_maxIntersectEdge;

		public Curve m_minEdge;

		public Curve m_minIntersectEdge;

		public XYZ m_vecMaxDirection;

		public XYZ m_vecMinDirection;
	}
}
